package C043;

/**
 * ClassName: C02
 * Package: C043
 * Description: 超级回文数
 *
 * @Author BCXJ
 * @Create 2025/2/6 21:22
 * @Version 1.0
 * @Since 1.0
 */
public class C02 {
    public static void main(String[] args) {
        System.out.println(superpalindromesInRange("4", "1000"));
    }

    public static int superpalindromesInRange(String left, String right) {
        long l = Long.parseLong(left);
        long r = Long.parseLong(right);
        long num = 1;
        long num2 = 0;
        long num3 = 0;
        int ans = 0;
        while(num <= 100000) {

            // 偶数个数的形式的判断
            num2 = evenEnlarge(num);
            if (process(num2 * num2, l, r)) {
                System.out.println(num2 * num2);
                ans ++;
            }
            // 奇数个数的形式的判断
            num3 = oddEnlarge(num);
            if(process(num3 * num3, l, r)) {
                System.out.println(num3 * num3);
                ans ++;
            }
            num ++;
        }
        return ans;
    }

    private static long oddEnlarge(long num) {
        long mid = num / 10;
        while(mid != 0) {
            num = num * 10 +  mid % 10;
            mid /= 10;
        }
        return num;
    }

    private static boolean process(long num2, long l, long r) {
        return num2 >= l && num2 <= r && isPalindrome(num2);
    }

    private static long evenEnlarge(long num) {
        long mid = num;
        while(mid != 0) {
            num = num * 10 +  mid % 10;
            mid /= 10;
        }
        return num;
    }

    public static  boolean isPalindrome(long num) {
        if(num < 0) {
            return false;
        }
        long offset = 1;
        while(num / offset >= 10) {
            offset *= 10;
        }

        while(num != 0) {
            if(num / offset != num % 10) {
                return false;
            }
            num = (num % offset) / 10;
            offset /= 100;
        }

        return true;
    }
}
